package rest.literaturplugin;

import java.util.List;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import model.literaturplugin.Book;

import util.WebServiceException;
import auth.SecurityManager;
import dao.GenericDAO;

@RequestScoped
@Path("/literaturplugin/v1")
public class LiteraturpluginWS {

	public LiteraturpluginWS() {
		super();
	}

	@EJB
	private GenericDAO genericDAO;

	@Inject
	private SecurityManager securityManager;
	
	@GET
	@Path("/Book")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Book> getAllPublicBooks(){
		return (List<Book>) genericDAO.findAllPublic(Book.class);
	}

	@POST
	@Path("/Book")
	@Consumes(MediaType.APPLICATION_JSON)
	public void postNewBook(Book book) {
		try {
			book.setId(null);
			genericDAO.create(book);
		} catch (WebServiceException e) {
			throw new WebApplicationException(Response.Status.BAD_REQUEST);
		}
	}

}